﻿using PMSystem.DataAccess.DataContext;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PMSystem.DataAccess.DataRepository
{
    public class RecordItemImageRepository : IRepository<RecordItemImage>
    {
        Context context = new Context();

        public IQueryable<RecordItemImage> GetAll
        {
            get { return context.RecordItemImages; }
        }

        public IQueryable<RecordItemImage> AllIncluding(params Expression<Func<RecordItemImage, object>>[] includeProperties)
        {
            IQueryable<RecordItemImage> query = context.RecordItemImages;
            foreach (var includeProperty in includeProperties)
            {
                // query = query.Include(includeProperty);
            }
            return query;
        }



        public void InsertOrUpdate(RecordItemImage record)
        {
            if (record.ImageId == default(int))
            {
                // New entity
                context.RecordItemImages.Add(record);
            }
            else
            {
                // Existing entity
                context.Entry(record).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var record = context.RecordItemImages.Find(id);
            context.RecordItemImages.Remove(record);
        }

        public virtual void DeleteList(List<RecordItemImage> items)
        {

            foreach (RecordItemImage item in items)
                {
                    context.Entry(item).State = EntityState.Deleted;
                }
                context.SaveChanges();
            
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }

        public RecordItemImage GetById(int id)
        {
            return context.RecordItemImages.Find(id);
        }

        public List<RecordItemImage> GetAllByRecordId(int id)
        {
            return context.RecordItemImages.Where(x => x.RecordId == id).ToList();
        }

        public List<RecordItemImage> GetAllByCondition(Expression<Func<RecordItemImage, bool>> predicate)
        {
            return context.RecordItemImages.Where(predicate).ToList();
        }

    }
}
